cmake_minimum_required(VERSION 3.10)

set(CMAKE_CXX_STANDARD 17)
project(guild_service_building)

aux_source_directory(. SRC_FILES)
aux_source_directory(${PROJECT_SOURCE_DIR}/logic SRC_FILES)
aux_source_directory(${PROJECT_SOURCE_DIR}/error SRC_FILES)
aux_source_directory(${PROJECT_SOURCE_DIR}/../../../../src/guildservicebuilding SRC_FILES)
aux_source_directory(${PROJECT_SOURCE_DIR}/../../../../src/guildservicebuilding/protobuf SRC_FILES) #pb
aux_source_directory(${PROJECT_SOURCE_DIR}/../../../../../../thirdparty/src/base64 SRC_FILES) #base64

if (GCOV)
  message("GCOV On")
  set_source_files_properties(${SRC_FILES} PROPERTIES COMPILE_FLAGS --coverage)
endif()


add_library(${PROJECT_NAME} ${SRC_FILES})
target_compile_definitions(${PROJECT_NAME} PUBLIC $<$<CONFIG:Debug>: -DDEBUG>)

set(INCLUDE_DIR
    .
    ${PROJECT_SOURCE_DIR}/../../include
    ${PROJECT_SOURCE_DIR}/../../../../src/include/guildservicetimer
    ${PROJECT_SOURCE_DIR}/../../../../src/include/guildservicebuilding
    ${PROJECT_SOURCE_DIR}/../../../../src/include/guildservicebuilding/protobuf
    ${PROJECT_SOURCE_DIR}/../../../../src/include/guildservicetimer/GuildServiceTimer/proxy
    ${PROJECT_SOURCE_DIR}/../../../../..
    ${PROJECT_SOURCE_DIR}/../../../../../../thirdparty
    ${PROJECT_SOURCE_DIR}/../../../../../../thirdparty/include
    ${PROJECT_SOURCE_DIR}/../../../../thirdparty/include
    ${PROJECT_SOURCE_DIR}/../../../../src/include/root
    /usr/local/include/hiredis
)
target_include_directories(${PROJECT_NAME} PRIVATE ${INCLUDE_DIR})

set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "lib")
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ".a")

set_target_properties(${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/build/lib)
set_target_properties(${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR}/build/Debug/lib)
set_target_properties(${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR}/build/Release/lib)

IF (MSVC)
    set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "/EHa /bigobj /wd4267 /wd4311 /wd4302 /wd4312 /wd4244 /wd4065 /wd4250")
    foreach(var
        CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
        CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
        CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
        CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO
    )
        if(${var} MATCHES "/MD")
            string(REGEX REPLACE "/MD" "/MT" ${var} "${${var}}")
        endif()
    endforeach()

    SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES LINK_FLAGS_DEBUG "
        /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/guildservicetimer/Debug/libguildservicetimer.a
        /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/proxy/guildservicetimer/GuildServiceTimer/Debug/libGuildServiceTimer_proxy.a
    ")
    SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES LINK_FLAGS_RELEASE "
        /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/guildservicetimer/Release/libguildservicetimer.a
        /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/proxy/guildservicetimer/GuildBuildingTimer/Release/libGuildServiceTimer_proxy.a
    ")
ELSE()
    set(COMPILER_FLAGS)
    set(COMPILER_FLAGS_DEBUG)
    set(COMPILER_FLAGS_RELEASE)
    if(CMAKE_CXX_COMPILER_ID MATCHES GNU)
        list(APPEND COMPILER_FLAGS "-fnon-call-exceptions" "-fPIC")
        list(APPEND COMPILER_FLAGS_DEBUG "-g" "-O0")
        list(APPEND COMPILER_FLAGS_RELEASE "-O2" "-Wall" "-Wunused-variable")
        set(CMAKE_CXX_COMPILER "g++")
    elseif(CMAKE_CXX_COMPILER_ID MATCHES Clang)
        list(APPEND COMPILER_FLAGS_FLAGS "-fnon-call-exceptions" "-fPIC")
        list(APPEND COMPILER_FLAGS_DEBUG "-g" "-O0")
        list(APPEND COMPILER_FLAGS_RELEASE "-O2" "-Wall" "-Wunused-variable")
        set(CMAKE_CXX_COMPILER "clang++")
    endif()

    target_link_libraries(${PROJECT_NAME} "$<$<BOOL:${GCOV}>:libgcov.a>")

    target_compile_options(${PROJECT_NAME}
        PRIVATE
        ${COMPILER_FLAGS}
        $<$<CONFIG:Debug>:${COMPILER_FLAGS_DEBUG}>
        $<$<CONFIG:Release>:${COMPILER_FLAGS_RELEASE}>
    )
ENDIF ()